﻿using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Sobiens.Connector.Admin.BLL
{
    [DataContract(Namespace = "", Name = "AppConfiguration1")]
    public class AppConfigurationData1
    {
        #region fields

        private static DataContractSerializer _dcs = new DataContractSerializer(typeof(Sobiens.Connectors.Entities.Settings.AppConfiguration));
        private static DataContractJsonSerializer _dcjs = new DataContractJsonSerializer(typeof(Sobiens.Connectors.Entities.Settings.AppConfiguration));

        #endregion

        #region properties

        [DataMember(Order = 1)]
        public Boolean DetailedLogMode { get; set; }

        [DataMember(Order = 2)]
        public SiteSetting[] SiteSettings { get; set; }

        [DataMember(Order = 3)]
        public FolderSetting[] FolderSettings { get; set; }

        [DataMember(Order = 4)]
        public DocumentTemplate[] DocumentTemplates { get; set; }

        [DataMember(Order = 5)]
        public DocumentTemplateMapping[] DocumentTemplateMappings { get; set; }

        [DataMember(Order = 6)]
        public ExplorerConfiguration ExplorerConfiguration { get; set; }
        
        [DataMember(Order = 7)]
        public String ExcelConfigurations { get; set; }

        [DataMember(Order = 8)]
        public String WordConfigurations { get; set; }

        [DataMember(Order = 9)]
        public OutlookConfigurations OutlookConfigurations { get; set; }

        #endregion


        public AppConfigurationData1()
        {
            DetailedLogMode = false;
            SiteSettings = new SiteSetting[0];
            FolderSettings = new FolderSetting[0];
            DocumentTemplates = new DocumentTemplate[0];
            DocumentTemplateMappings = new DocumentTemplateMapping[0];
            ExplorerConfiguration = new ExplorerConfiguration();
            OutlookConfigurations = new OutlookConfigurations();
        }

        #region public methods

        public String GetXML()
        {
            return Serialize(_dcs);
        }

        public String GetJSON()
        {
            return Serialize(_dcjs);
        }

        public static Sobiens.Connectors.Entities.Settings.AppConfiguration CreateFromJSON(String json)
        {
            return Deserialize(_dcjs, json);
        }

        public static Sobiens.Connectors.Entities.Settings.AppConfiguration CreateFromXML(String xmlData)
        {
            return Deserialize(_dcs, xmlData);
        }

        public void ProcessNewElements()
        {
       
        }

        public void ProcessDeletedElements()
        {
            SiteSettings = SiteSettings.Where(ss => ss.IsDeleted == false).ToArray();
            DocumentTemplates = DocumentTemplates.Where(dt => dt.IsDeleted == false).ToArray();
            DocumentTemplateMappings = DocumentTemplateMappings.Where(dtm => dtm.IsDeleted == false).ToArray();
        }


        #endregion

        #region private methods

        private static Sobiens.Connectors.Entities.Settings.AppConfiguration Deserialize(XmlObjectSerializer serializer, String data)
        {
            Sobiens.Connectors.Entities.Settings.AppConfiguration appConfig = String.IsNullOrEmpty(data)
                ? new Sobiens.Connectors.Entities.Settings.AppConfiguration()
                : serializer.ReadObject(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(data))) as Sobiens.Connectors.Entities.Settings.AppConfiguration;
            return appConfig;
        }

        private String Serialize(XmlObjectSerializer serializer)
        {
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, this);
            return Encoding.UTF8.GetString(stream.ToArray());
        }

        #endregion
    }
}
